/*
 * Client.java
 *
 * 
 */

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client {
	static public void main(String args[]) {

		try {
			
			RemoteInterface rmiServer;
			Registry registry;
			String name = args[0];
			int s_port = Integer.parseInt(args[1]);

			// Prompt setup
			System.out.println("---------------------------------------");
			System.out.println("Possible commands:");
			System.out.println("	lookup <day>");
			System.out.println("	reserve <day>");
			System.out.println("---------------------------------------");
			System.out.print("Enter command : ");

			Scanner userInput = new Scanner(System.in);
			while (userInput.hasNextLine()) 
			{
				// Prompt setup
				System.out.println("---------------------------------------");

				registry = LocateRegistry.getRegistry("localhost", s_port);
				rmiServer = (RemoteInterface) (registry.lookup(name));

				String input = userInput.nextLine();
				String[] array = input.split(" ");
				String request_type = array[0];
				int day_number = Integer.parseInt(array[1]);

				if (input.equals("exit")) 
				{
					System.out.println("Client exiting!");
					System.exit(0);
				} 
				else if (request_type.equals("lookup")) 
				{
					System.out.println("Sending lookup(" + day_number + ") to the Server");

					// response = rmiServer.additionService(input);
					String response = rmiServer.lookup(day_number);

					// Show all results
					System.out.println();
					System.out.println("******Search Results******");
					System.out.println(response);
					System.out.println("**************************");
					System.out.println();
				}
				System.out.println("---------------------------------------");
				System.out.print("Enter command : ");
			}
			userInput.close();
		} catch (RemoteException | NotBoundException e) {
			// e.printStackTrace();
			System.out.println("ERROR: Client has no Server Name: " + args[0] + " to connect to! Client is Exiting!");
			System.exit(0);
		}

	}

}